package com.zr.d_c.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zr.d_c.model.User;
import com.zr.d_c.service.DishService;
import com.zr.d_c.service.SellerService;
import com.zr.d_c.serviceimpl.DishServiceImpl;
import com.zr.d_c.serviceimpl.SellerServiceImpl;

import net.sf.json.JSONObject;
/**
 * 查找菜品
 * @author Administrator
 *
 */
public class FindDishController extends HttpServlet{
	DishService dishService = new DishServiceImpl();
	SellerService sellerser = new SellerServiceImpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		resp.setCharacterEncoding("utf8");
		JSONObject  dishobj  = new JSONObject();
		HttpSession   session = req.getSession();
		PrintWriter  pw = resp.getWriter();
		String dname = req.getParameter("searchname");
		int page = Integer.parseInt(req.getParameter("page"));
		int pageSize = Integer.parseInt(req.getParameter("rows"));
		int start =  (page-1)*pageSize;
		User u = (User) session.getAttribute("currentUser");
		int uid = u.getUid();
		int sid = sellerser.getSellerSidByUid(uid);
		if(dname!=null){
			dishobj = dishService.getDishBySidAndDname(sid, dname, start, pageSize);
		}else{
			dishobj = dishService.getDishBySid(sid, start, pageSize);
		}
		pw.write(dishobj.toString());

	}
}
